//
//  ResultStatisticsView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/11.
//  Copyright © 2019 李文强. All rights reserved.
//

#import "ResultStatisticsView.h"
#import "SYRequestAPI+Teach.h"

@implementation ResultStatisticsView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame andClassID:(NSString *)classID andePaperID:(NSString *)paperID{
    if ([super initWithFrame:frame]) {
        self.classID = classID;
        self.paperID = paperID;
        [self initSubView];
        [self getAnXueShengTongJiRequest];
//        [self getAnShiTiTongJiRequest];
    }
    return self;
}

- (void)initSubView{
    type = 0;
    pageIndexForShiTi=1;
    pageSizeForShiTi=30;
    pageIndexForXueSheng=1;
    pageSizeForXueSheng=30;
    _xueShengTongJiArray = [NSMutableArray new];
    _shiTiTongJiArray = [NSMutableArray new];
    [self addSubview:self.switchButton];
    [_switchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.height.equalTo(@44);
    }];
    [self addSubview:self.tableList];
    [_tableList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@44);
        make.bottom.equalTo(@0);
    }];
    [self getAnXueShengTongJiRequest];
    
    [_tableList headerRefreshBlock:^{
        if (self->type == 0) {
            self->pageIndexForXueSheng = 1;
            [self->_xueShengTongJiArray removeAllObjects];
            [self getAnXueShengTongJiRequest];
        }else{
            self->pageIndexForShiTi = 1;
             [self->_shiTiTongJiArray removeAllObjects];
            [self getAnShiTiTongJiRequest];
        }
    }];
    [_tableList fooderRefreshBlock:^{
        if (self->type == 0) {
            self->pageIndexForXueSheng ++;
            [self getAnXueShengTongJiRequest];
        }else{
            //z按照试题统计
            self->pageIndexForShiTi ++;
            [self getAnShiTiTongJiRequest];
        }
    }];
}
- (RSSwitchButtonView *)switchButton{
    if (!_switchButton) {
        RSSwitchButtonView *view = [[RSSwitchButtonView alloc]initWithFrame:CGRectMake(0, self.frame.size.width, 0, 44) andButtonTitle:@[@"按学生统计",@"按试题统计"]];
        view.backgroundColor = [UIColor whiteColor];
        view.delegate = self;
        _switchButton = view;
    }
    return _switchButton;
}
- (UITableView *)tableList{
    if (!_tableList) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 0) style:UITableViewStylePlain];
        tableView.delegate  =self;
        tableView.dataSource = self;
        [tableView registerClass:[RSVTableViewCell class] forCellReuseIdentifier:@"RSVTableViewCell"];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableList = tableView;
    }
    return _tableList;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (type == 0) {
        return _xueShengTongJiArray.count;
    }else{
        return _shiTiTongJiArray.count;
    }
    return 10;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RSVTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RSVTableViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[RSVTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RSVTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row%2 == 0) {
        cell.backgroundColor = [UIColor whiteColor];
    }else{
        cell.backgroundColor = [UIColorChange colorwithHexString:@"#F8F8F8"];
    }
    [cell reloadViewLayout];
    UILabel *labelNameLabel = (UILabel *)[cell viewWithTag:20];//姓名
    UILabel *labelRightWoringLabel = (UILabel *)[cell viewWithTag:21];//最对/错
    UILabel *labelZhengQueLvLabel = (UILabel *)[cell viewWithTag:22];//正确率
    UILabel *labelDeFenLvLabel = (UILabel *)[cell viewWithTag:23];//得分率
    UILabel *labelScoreLabel = (UILabel *)[cell viewWithTag:24];//得分
    UILabel *labelPaiMingLabel = (UILabel *)[cell viewWithTag:25];//排名
    [cell.checkButton addTarget:self action:@selector(chickClick:) forControlEvents:UIControlEventTouchUpInside];//操作
    cell.checkButton.tag = 1+indexPath.row;
    if (type == 0) {
        JiaoXueAnXueShengTongJiModel *model = _xueShengTongJiArray[indexPath.row];
        labelNameLabel.text = model.name;
        labelRightWoringLabel.text = [NSString stringWithFormat:@"%@/%@",model.rightNums,model.wrongNums.length>0?model.wrongNums:@"0"];
        labelZhengQueLvLabel.text = model.rightRatio;
        labelDeFenLvLabel.text = model.scoreRatio;
        labelScoreLabel.text = model.score;
        labelPaiMingLabel.text = model.ranking;
    }else{
        JiaoXueAnTiJiaoXiangQingTiJiaoModel *model = _shiTiTongJiArray[indexPath.row];
        labelNameLabel.text = model.questionSerial;//题号
        labelRightWoringLabel.text = model.questionType;//题型
        labelZhengQueLvLabel.text = model.rightStudents;//作对人数
        labelDeFenLvLabel.text = model.wrongStudents;//做错人数
        labelScoreLabel.text = model.rightRatio;//正确率
        labelPaiMingLabel.text = model.wrongReason;//标签
    }
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
     if (type == 0) {
         if (!_titleView) {
            _titleView = [[SJXQHeaderTitleView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 44) andArray:@[@"姓名",@"作对/错",@"正确率",@"得分率",@"得分",@"排名",@"操作"]];
             _titleView.backgroundColor = CONTENTBACKCOLOR;
         }
         [_titleView reloadView:@[@"姓名",@"作对/错",@"正确率",@"得分率",@"得分",@"排名",@"操作"]];
     }else{
         [_titleView reloadView:@[@"题号",@"题型",@"做对人数",@"做错人数",@"正确率",@"标签",@"操作"]];
     }
    
    return _titleView;
}
- (void)chickClick:(UIButton *)sender{
    int tag = (int)sender.tag-1;
    if (type == 0) {
        //按照学生统计
        JiaoXueAnXueShengTongJiModel *model = _xueShengTongJiArray[tag];
//        [self.mainDelegate rSD:model.questionId];
        [self.mainDelegate anXueShengTongJiDelegate:model.xsid];
    }else{
        //z按照试题统计
        JiaoXueAnTiJiaoXiangQingTiJiaoModel *model = _shiTiTongJiArray[tag];
        [self.mainDelegate rSD:model.questionId];
    }
}
#pragma mafk - 按学生统计request
- (void)getAnXueShengTongJiRequest{
    NSDictionary *parama = @{@"classId":self.classID,
                             @"paperId":self.paperID,
                             @"pageIndex":[NSString stringWithFormat:@"%d",pageIndexForXueSheng],
                             @"pageSize":[NSString stringWithFormat:@"%d",pageSizeForXueSheng]};
    
    [SYRequestAPI sy_getHomeWordServiceCountByStudentList:parama success:^(id  _Nonnull responseObject) {
        NSLog(@"按照学生统计结果  %@",responseObject);
        if (self->pageIndexForXueSheng == 1) {
            [self->_xueShengTongJiArray removeAllObjects];
        }
        NSArray *array = [responseObject objectForKey:@"data"];
        for (NSDictionary *dic in array) {
            JiaoXueAnXueShengTongJiModel *model = [JiaoXueAnXueShengTongJiModel yy_modelWithJSON:dic];
            [self->_xueShengTongJiArray addObject:model];
        }
//        self->pageIndexForXueSheng++;
        [self.tableList reloadData];
        [self.tableList.mj_header endRefreshing];
        [self.tableList.mj_footer endRefreshing];
    } fail:^(NSError *error) {
        [self.tableList.mj_header endRefreshing];
        [self.tableList.mj_footer endRefreshing];
    }];
}
- (void)getAnShiTiTongJiRequest{
    NSDictionary *parama = @{@"classId":self.classID,
                             @"paperId":self.paperID,
                             @"pageIndex":[NSString stringWithFormat:@"%d",pageIndexForShiTi],
                             @"pageSize":[NSString stringWithFormat:@"%d",pageSizeForShiTi]};
    
    [SYRequestAPI sy_getHomeWordServiceCountByQuestionList:parama success:^(id  _Nonnull responseObject) {
        NSLog(@"按照试题统计结果  %@",responseObject);
        if (self->pageIndexForShiTi == 1) {
            [self->_shiTiTongJiArray removeAllObjects];
        }
        NSArray *array = [responseObject objectForKey:@"data"];
        for (NSDictionary *dic in array) {
            JiaoXueAnTiJiaoXiangQingTiJiaoModel *model = [JiaoXueAnTiJiaoXiangQingTiJiaoModel yy_modelWithJSON:dic];
            [self->_shiTiTongJiArray addObject:model];
        }
        //self->pageIndexForShiTi++;
        [self.tableList reloadData];
        [self.tableList.mj_header endRefreshing];
        [self.tableList.mj_footer endRefreshing];
    } fail:^(NSError *error) {
        [self.tableList.mj_header endRefreshing];
        [self.tableList.mj_footer endRefreshing];
    }];
}
-(void)rSSwitchButtonViewSwitch:(int)index{
    if (index == 0) {
        type = 0;
        [self.tableList reloadData];
    }else{
        type = 1;
        if (_shiTiTongJiArray.count == 0) {
            [self getAnShiTiTongJiRequest];
        }
        [self.tableList reloadData];
    }
}
@end
